package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 四川在线
 * @author Administrator
 * abc
 */
public class ScolNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*[.]scol.com.cn/.*/content/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]+.htm",
				"http://.*[.]scol.com.cn/.*/content/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]+.htm[?]node=[0-9]+",
				"http://.*[.]scol.com.cn/html/[0-9]{4}/[0-9]{2}/[0-9]+[_][0-9]+.shtml",
				"http://[a-z]+.scol.com.cn/[a-z]+/[0-9\\/]+.html"
		};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (!isDetailPage(urlMeta.getUrl())) {
		}
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";;
		String author="";
		String url=urlMeta.getUrl();
		Long date=null;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		doc.select("#displaypagenum").remove();
		/*
		 * 解析新闻标题
		 */
		ParseTool tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("#webreal_scol_title"));
		title=tool.parse();


		/*
		 * 解析时间
		 */
		tool.addParseLogic(new ParseLogic("#scol_time"));
		tool.addParseLogic(new ParseLogic("td.t10"));
		date=tool.parseDate();


		/*
		 * 解析内容
		 */
		tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("div#scol_txt"));
		tool.addParseLogic(new ParseLogic("td.t11"));
		content=tool.parse();

		/*
		 * 解析作者
		 */
		doc.select("#Span_CommentCount").remove();
		author= JSoupUtils.matchAuthor(doc, "来源：");
		if(StringUtils.isBlank(author))
			author=doc.select("#scol_time a").text();
		if(StringUtils.isNotBlank(author)){
			author=author.trim();
		}

		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setAuthor(author);

		return meta;


	}
	public static void main(String[] args) {
		ScolNewsAnalyse hsw=new ScolNewsAnalyse();
//		String url="http://news.scol.com.cn/gdxw/content/2012-11/22/content_4349121.htm?node=2856";
//		String url="http://money.scol.com.cn/html/2012/11/017037_1052881.shtml";
		String url="http://news.scol.com.cn/gdxw/201604/54494388.html";
//		String url="http://focus.scol.com.cn/shwx/content/2012-11/22/content_4350742.htm?node=527";
//		String url="http://focus.scol.com.cn/gjyw/content/2012-11/22/content_4351390.htm?node=538";
//		String url="http://focus.scol.com.cn/zgsz/content/2012-11/22/content_4351170.htm?node=540";
//		String url="http://focus.scol.com.cn/jrgz/content/2012-11/19/content_4331822.htm?node=511";
//		String url="http://comment.scol.com.cn/html/2012/11/011009_1052602.shtml";
//		String url="http://ent.scol.com.cn/schlw/content/2012-11/24/content_4361541.htm?node=886";
		System.out.println(hsw.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=hsw.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
